package com.fastjrun.eladmin.itrms.service.impl;

import com.fastjrun.eladmin.itrms.common.exception.ServiceException;
import com.fastjrun.eladmin.itrms.dao.RmsServerDao;
import com.fastjrun.eladmin.itrms.dto.ServerDTO;
import com.fastjrun.eladmin.itrms.dto.ServerQueryDTO;
import com.fastjrun.eladmin.itrms.entity.RmsServer;
import com.fastjrun.eladmin.itrms.entity.RmsServerExample;
import com.fastjrun.eladmin.itrms.mapstruct.RmsServerMapper;
import com.fastjrun.eladmin.itrms.service.RmsServerControllerService;
import com.fastjrun.eladmin.itrms.utils.PageUtils;
import com.fastjrun.eladmin.itrms.vo.ServerVO;
import com.fastjrun.eladmin.vo.PageResult;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@Service("rmsServerControllerService")
public class RmsServerControllerServiceImpl extends BaseItrmsService implements RmsServerControllerService {
    @Resource
    RmsServerDao rmsServerDao;

    @Resource
    RmsServerMapper rmsServerMapper;

    private RmsServerExample retrive(ServerQueryDTO queryDTO){
        log.debug("queryDTO={}",queryDTO);
        RmsServerExample rmsServerSelectExample=new RmsServerExample();
        RmsServerExample.Criteria  criteria=rmsServerSelectExample.createCriteria();
        if(queryDTO!=null){
            if(queryDTO.getStatus()!=null){
                criteria.andStatusEqualTo(queryDTO.getStatus());
            }
        }
        return rmsServerSelectExample;
    }

    @Override
    public void export(HttpServletResponse response, ServerQueryDTO queryDTO){
        RmsServerExample rmsServerExample=this.retrive(queryDTO);
        List<RmsServer> all = rmsServerDao.selectByExampleWithBLOBs(rmsServerExample);
        List<ServerVO> serverVOS=this.rmsServerMapper.toVO(all);
        List<Map<String,Object>> list= Lists.newArrayList();
        for (RmsServer server : all) {
            Map<String,Object> map = Maps.newHashMap();
            map.put("服务器编号", server.getServerNo());
            map.put("服务器名称", server.getServerName());
            map.put("服务器类型", server.getServerType());
            map.put("是否分配", server.getStatus());
            map.put("公网IP", server.getPublicIp());
            map.put("内网IP", server.getPrivateIp());
            map.put("操作系统", server.getOs());
            map.put("cpu", server.getCpu());
            map.put("内存", server.getMemory());
            map.put("磁盘", server.getDisk());
            map.put("机密信息", server.getSecurityInfo());
            map.put("备注", server.getRemark());
            map.put("负责用户", server.getOwnerUser());
            map.put("创建用户", server.getCreateUser());
            map.put(" 创建时间",  server.getCreateDate());
            map.put(" 更新时间",  server.getUpdateDate());
            list.add(map);
        }
        try {
            FileUtil.downloadExcel(list, response);
        } catch (IOException e) {
            log.warn("",e);
            throw new ServiceException(e);
        }

    }

    @Override
    public PageResult<ServerVO> query(ServerQueryDTO queryDTO, Pageable pageable) {
        if(pageable==null){
            pageable=PageRequest.of(PAGE_CURRENT,PAGE_SIZE);
        }


        PageUtils.startPage(pageable);


        RmsServerExample rmsServerExample=this.retrive(queryDTO);
        String orderClause=this.getOrderClause(pageable);
        if(StringUtils.isNotBlank(orderClause)){
            rmsServerExample.setOrderByClause(orderClause);
        }



        PageInfo<RmsServer> pageInfo = new PageInfo<>(rmsServerDao.selectByExampleWithBLOBs(rmsServerExample));
        List<ServerVO> list = rmsServerMapper.toVO(pageInfo.getList());

        PageResult<ServerVO> pageResult = new PageResult<>();
        pageResult.setContent(list);
        pageResult.setTotalElements((int) pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public void create(ServerDTO createDTO) {
        RmsServer rmsServer=this.rmsServerMapper.toEntity(createDTO);
        rmsServer.setCreateUser(SecurityUtils.getCurrentUsername());
        rmsServer.setCreateUserId(SecurityUtils.getCurrentUserId());
        rmsServer.setOwnerUser(sysUserDao.selectByPrimaryKey(createDTO.getOwnerUserId()).getUsername());
        rmsServer.setCreateDate(new Date());
        rmsServer.setUpdateDate(new Date());
        rmsServerDao.insert(rmsServer);

    }

    @Override
    public void update(ServerDTO updateDTO) {
        RmsServer rmsServer=this.rmsServerMapper.toEntity(updateDTO);
        rmsServer.setOwnerUser(sysUserDao.selectByPrimaryKey(updateDTO.getOwnerUserId()).getUsername());
        rmsServer.setUpdateDate(new Date());
        RmsServer rmsServerOld=rmsServerDao.selectByPrimaryKey(rmsServer.getId());
        rmsServer.setCreateDate(rmsServerOld.getCreateDate());
        rmsServer.setCreateUserId(rmsServerOld.getCreateUserId());
        rmsServer.setCreateUser(sysUserDao.selectByPrimaryKey(rmsServer.getCreateUserId()).getUsername());
        rmsServerDao.updateByPrimaryKey(rmsServer);

    }

    @Override
    public void delete(Long[] ids) {
        RmsServerExample rmsServerExample = new RmsServerExample();
        rmsServerExample.createCriteria().andIdIn(Arrays.asList(ids));
        rmsServerDao.deleteByExample(rmsServerExample);
    }
}
